package com.easyfun.healthmagicbox.bluetooth.enbp;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Message;
import android.util.Log;

import com.easyfun.healthmagicbox.bluetooth.BTMessageHandler;
import com.easyfun.healthmagicbox.bluetooth.BluetoothChatService;
import com.easyfun.healthmagicbox.inputviews.HardwareInputActivity;
import com.easyfun.healthmagicbox.ormlite.HealthInputTypeEnum;
import com.easyfun.healthmagicbox.pojo.HealthInputData;
import com.easyfun.healthmagicbox.util.HealthDateUtil;
import com.easyfun.healthmagicbox.widget.zhuanpanView;


public class EnBloodPressureHandler extends BTMessageHandler {
	private static final String TAG = EnBloodPressureHandler.class.getSimpleName();
	
	public EnBloodPressureHandler(String personID, Context context,HardwareInputActivity parent) {
		super(personID, context,parent);
		previousMsgType = EnBloodPressureMsgType.NONE;
	}
	EnBloodPressureParser parser = new EnBloodPressureParser();
	
	
	EnBloodPressureMsgType previousMsgType = EnBloodPressureMsgType.NONE;
	
	protected void OnStateChanged(Message msg)
	{
		super.OnStateChanged(msg);
		switch (msg.arg1) {
        case BluetoothChatService.STATE_CONNECTED:
        	previousMsgType = EnBloodPressureMsgType.NONE;
		}
	}
	/**
	 * Not thread safty
	 * 
	 * message header: 0xFD, 0xFD, end: 0x0D, 0x0A
	 * 
	 * 1. BP sends 0xA5
	 * 2. Phone sends 0xFD, 0xFD, 0xFA, 0x0D, 0x0D, 0x0A
	 * 3. BP sends 0xFD, 0xFD, 0x06, 0x0D, 0x0A
	 * ... 
	 * 4. begins to send for LED board:	[0xFD,0xFD,0xFB,PressureH, PressureL,0X0D, 0x0A]
	 * PressureH*256+PressureL
	 * 5。[0xFD,0xFD,0xFC, SYS,DIA,PUL, 0X0D, 0x0A] for result
	 * OR
	 * [0xFD,0xFD,0xFD,0x0E, 0X0D, 0x0A] error
	 * SYS为收缩压,DIA为舒张压,PUL为心率. 这个数据血压会连续传5次.
	 * 
	 * 6. phone sends [0xFD,0xFD,0xFE, 0x06, 0x0D, 0x0A] to shutoff BP
	 * 7. BT replies: [0xFD,0xFD,0x07, 0x0D, 0x0A]
	 */	
	@Override
	protected void OnMessageRead(byte[] msg, int len) {
		EnBloodPressureMessage parsedMsg = parser.parse(msg,len);
		if (parsedMsg == null)
		{
			return;
		}
		byte[] response;
		switch (parsedMsg.getMsgType())
		{
		case START:
			
			if ((previousMsgType == EnBloodPressureMsgType.NONE)||
					(previousMsgType == EnBloodPressureMsgType.SUCCESSFULRESULT)||
						(previousMsgType == EnBloodPressureMsgType.ERRORRESULT))
			{
				response = new byte[]{(byte) 0xFD, (byte) 0xFD, (byte) 0xFA, 
						(byte) 0x05, (byte) 0x0D, (byte) 0x0A};
				super.write(response);
			}
			previousMsgType = EnBloodPressureMsgType.START;
			break;
		case CONNECTED:
			previousMsgType = EnBloodPressureMsgType.CONNECTED;
			break;
		case SENDING:
			inProgress(parsedMsg);
			previousMsgType = EnBloodPressureMsgType.SENDING;
			break;
		case SUCCESSFULRESULT:
			//to avoid duplicated message
			if (previousMsgType == EnBloodPressureMsgType.SENDING)
			{
				response = new byte[]{(byte) 0xFD, (byte) 0xFD, (byte) 0xFE, 
						(byte) 0x06, (byte) 0x0D, (byte) 0x0A};
				//process correct
				processSuccess(parsedMsg);
				super.write(response);
			}
			previousMsgType = EnBloodPressureMsgType.SUCCESSFULRESULT;
			break;
		case ERRORRESULT:
			if (previousMsgType == EnBloodPressureMsgType.SENDING)
			{
				response = new byte[]{(byte) 0xFD, (byte) 0xFD, (byte) 0xFE, 
						(byte) 0x06, (byte) 0x0D, (byte) 0x0A};
				processError(parsedMsg);
				super.write(response);
			}
			previousMsgType = EnBloodPressureMsgType.ERRORRESULT;
			//process error
			break;
		default:
			previousMsgType = EnBloodPressureMsgType.NONE;
			Log.e(TAG, "error in bt msg handler!");
		}
		
	}
	
	private void processError(EnBloodPressureMessage parsedMsg) {
		Message message = new Message();   
        message.what = parent.BT_RESULT_ERROR;   
        message.obj = parsedMsg.getErrorCode();
		parent.uiHandler.sendMessage(message);	
		
	}
	private void inProgress(EnBloodPressureMessage parsedMsg) {
		Integer hp = Integer.valueOf(parsedMsg.getRunningLowPressure());
		
		Message message = new Message();   
        message.what = parent.BT_INPROGRESSING;   
        message.arg1 = hp;
		parent.uiHandler.sendMessage(message);	
	}
	
	private void processSuccess(EnBloodPressureMessage parsedMsg) {
		HealthInputData hid1 = new HealthInputData();
		long dt = HealthDateUtil.getDate(
				HealthDateUtil.getCurrentDateStr(null), "yyyy-MM-dd HH:mm")
				.getTime();		
		hid1.setValueTime(dt);
		hid1.setPersonID(m_personid);
		hid1.setValueData(parsedMsg.getHighPressure());
		hid1.setValueType(HealthInputTypeEnum.XUEYA_DIYA.toInteger());		
		sound(hid1);

		HealthInputData hid2 = new HealthInputData();
		hid2.setValueTime(dt);
		hid2.setPersonID(m_personid);
		hid2.setValueData(parsedMsg.getLowPressure());
		hid2.setValueType(HealthInputTypeEnum.XUEYA_GAOYA.toInteger());
		sound(hid2);

		HealthInputData hid3 = new HealthInputData();
		hid3.setValueTime(dt);
		hid3.setPersonID(m_personid);
		hid3.setValueData(parsedMsg.getHeartRate());
		hid3.setValueType(HealthInputTypeEnum.XINLV.toInteger());
		sound(hid3);
		
		List<HealthInputData> hids = new ArrayList<HealthInputData>();
		hids.add(hid1);
		hids.add(hid2);
		hids.add(hid3);
		Message message = new Message();   
        message.what = parent.BT_RESULT_ARRIVED;   
        message.obj = hids;
		parent.uiHandler.sendMessage(message);	
	}

	@Override
	protected void OnMessageWrite(byte[] msg, int len) {
		// TODO Auto-generated method stub
		
	}
	
	
}
